package com.guldan.core.search;

/**
 * Created by Silas.
 * Date: 2016/10/1
 * Time: 22:19
 */
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Pagination<T> implements Serializable {
    private static final long serialVersionUID = -3509205611665142708L;
    private int currentPage;
    private int currentPageIndex;
    private int pageSize;
    private int totalCount;
    private int pageCount;
    private int offset;
    private List<T> list;
    public static final int DEFAULT_PAGE_SIZE = 20;
    public static final int DEFAULT_CURRENT_PAGE = 1;

    public Pagination() {
        this.init((Integer)null, (Integer)null);
    }

    public Pagination(Integer currentPage) {
        this.init(currentPage, (Integer)null);
    }

    public Pagination(Integer currentPage, Integer pageSize) {
        this.init(currentPage, pageSize);
    }

    private void init(Integer currentPage, Integer pageSize) {
        this.currentPage = currentPage != null && currentPage.intValue() >= 1?currentPage.intValue():1;
        this.pageSize = pageSize != null && pageSize.intValue() >= 1?pageSize.intValue():20;
        this.currentPageIndex = this.getCurrentPage() - 1;
        this.offset = this.getCurrentPageIndex() * this.getPageSize();
        this.list = new ArrayList();
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
        this.pageCount = totalCount % this.pageSize == 0?totalCount / this.pageSize:totalCount / this.pageSize + 1;
        if(this.pageCount != 0 && this.currentPage > this.pageCount) {
            this.currentPage = this.pageCount;
        }

        this.currentPageIndex = this.getCurrentPage() - 1;
        this.offset = this.getCurrentPageIndex() * this.getPageSize();
    }

    public int getCurrentPage() {
        return this.currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage < 1?1:currentPage;
    }

    public int getCurrentPageIndex() {
        return this.currentPageIndex;
    }

    public int getPageSize() {
        return this.pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize < 1?20:pageSize;
    }

    public int getTotalCount() {
        return this.totalCount;
    }

    public int getPageCount() {
        return this.pageCount;
    }

    public List<T> getList() {
        return this.list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public int getOffset() {
        return this.offset;
    }
}

